package com.hsy.linemoney.modules.my;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AccountService;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.beans.User;
import com.hsy.linemoney.common.Config;
import com.hsy.linemoney.modules.auth.activity.LoginActivity;
import com.hsy.linemoney.modules.common.CommonBaseFragment;
import com.hsy.linemoney.modules.my.gain_and_reward.MyGainAndRewardActivity;
import com.hsy.linemoney.modules.main.MainActivity;
import com.hsy.linemoney.modules.my.cash_and_report.MyCashAndReportActivity;
import com.hsy.linemoney.modules.my.myaccount.MyAccountActivity;
import com.hsy.linemoney.modules.my.mypoints.MyPointsActivity;
import com.hsy.linemoney.modules.my.mycustomer.MyCustomerActivity;
import com.hsy.linemoney.modules.my.mydeals.MyDealsActivity;
import com.hsy.linemoney.modules.my.myteam.MyTeamActivity;
import com.hsy.linemoney.modules.my.status_and_record.MyStatusAndRecordActivity;

/**
 * 【我的】页面
 * Created by Administrator on 2016/6/23.
 */
public class MyFragment extends CommonBaseFragment implements View.OnClickListener {
    protected TextView myName;
    protected TextView myId;
    protected TextView myEmail;

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        RelativeLayout myAccount = findViewById(R.id.my_account);
        RelativeLayout myDeals = findViewById(R.id.my_deals);
        RelativeLayout myPoints = findViewById(R.id.my_points);
        RelativeLayout statusAndRecord = findViewById(R.id.status_and_record);
        RelativeLayout cashAndReport = findViewById(R.id.cash_and_report);
        RelativeLayout myTeam = findViewById(R.id.my_team);
        RelativeLayout myGain = findViewById(R.id.my_gain);
        RelativeLayout myAgentGain = findViewById(R.id.my_team_gain);
        RelativeLayout settingRelative = findViewById(R.id.setting_relative);
        RelativeLayout leave = findViewById(R.id.leave);

        myAccount.setOnClickListener(this);
        myDeals.setOnClickListener(this);
        myPoints.setOnClickListener(this);
        statusAndRecord.setOnClickListener(this);
        cashAndReport.setOnClickListener(this);
        myTeam.setOnClickListener(this);
        myGain.setOnClickListener(this);
        settingRelative.setOnClickListener(this);
        myAgentGain.setOnClickListener(this);
        leave.setOnClickListener(this);

        myName = F(R.id.my_name);
        myId = F(R.id.my_id);
        myEmail = F(R.id.my_email);

        User user = LineMoneyContext.getLoggedUser();
        myName.setText(user.getFirstName() + " " + user.getLastName());
        myId.setText(user.getUserId());
        myEmail.setText(user.getEmailAddress());

        AccountService service = new AccountService();
        service.getCashableAccounts(null, false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_relative:
                final String[] items = Config.getAvailableLangNameArray();
                AlertDialog.Builder dialog = new AlertDialog.Builder(getAttchedActivity()).setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LineMoneyContext.clearData();

                        LineMoneyContext.setLocalLanguagePair(Config.getAvailableLang(which));
                        dialog.dismiss();
                        startActivityWithTaskCleared(MainActivity.class);
                    }
                });
                dialog.show();
                break;
            case R.id.leave:
                final Dialog builder = new AlertDialog.Builder(getAttchedActivity()).create();
                builder.setCancelable(true);
                builder.show();
                LayoutInflater layoutInflater = LayoutInflater.from(getAttchedActivity());
                View view = layoutInflater.inflate(R.layout.dialog_exit, null);
                TextView cancel = (TextView) view.findViewById(R.id.cancel);
                TextView ok = (TextView) view.findViewById(R.id.ok);
                builder.setContentView(view);

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        builder.dismiss();
                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        builder.dismiss();
                        LineMoneyContext.logout();
                        startActivityWithTaskCleared(LoginActivity.class);
                    }
                });
                break;

            case R.id.my_account:
                startActivity(MyAccountActivity.class);
                break;
            case R.id.my_deals:
                startActivity(MyDealsActivity.class);
                break;
            case R.id.my_points:
                startActivity(MyPointsActivity.class);
                break;
            case R.id.status_and_record:
                startActivity(MyStatusAndRecordActivity.class);
                break;
            case R.id.cash_and_report:
                startActivity(MyCashAndReportActivity.class);
                break;
            case R.id.my_gain:
                startActivity(MyGainAndRewardActivity.class);
                break;
            case R.id.my_team:
                startActivity(MyTeamActivity.class);
                break;
            case R.id.my_team_gain:
                startActivity(MyCustomerActivity.class);
                break;
        }
    }
}
